// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.save;

import java.util.Iterator;
import java.util.Vector;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import tc.common.utility.Pair;
import tc.crase.db.AttrId;
import tc.crase.db.Xattr;
import tc.crase.db.XattrGIT;
import tc.crase.db.XattrGRP;
import tc.crase.db.XattrGXY;
import tc.crase.db.XattrLNK;
import tc.crase.db.XattrSTR;
import tc.crase.db.XattrTPE;
import tc.crase.db.XattrURL;
import tc.crase.db.Xobject;
import tc.crase.sys.XdbEx;
import tc.crase.sys.XdbRoot;
import tc.crase.sys.XobjectEx;

public class XdbSaveDb {
	static public void saveAttr(Xattr attr, ArrayNode jsonArray, ObjectMapper mapper) {
		if (attr.getAsSTR() != null) {
			saveAttrSTR(attr.getAsSTR(), jsonArray, mapper);
		}
		else if (attr.getAsLNK() != null) {
			saveAttrLNK(attr.getAsLNK(), jsonArray, mapper);
		}
		else if (attr.getAsTPE() != null) {
			saveAttrTPE(attr.getAsTPE(), jsonArray, mapper);
		}
		else if (attr.getAsGXY() != null) {
			saveAttrGXY(attr.getAsGXY(), jsonArray, mapper);
		}
		else if (attr.getAsGRP() != null) {
			saveAttrGRP(attr.getAsGRP(), jsonArray, mapper);
		}
		else if (attr.getAsURL() != null) {
			saveAttrURL(attr.getAsURL(), jsonArray, mapper);
		}
	}
	
	static public void saveAttrGRP(XattrGRP attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("type",  "GRP");
		userOb.put("id",   attr.getId());
		
		ArrayNode jsonItems = mapper.createArrayNode();
		userOb.put("items2", jsonItems);
		
		for (int i = 0; i < attr.items.size(); i++) {
			Pair<AttrId, XattrGIT> item = attr.items.get(i);
			ObjectNode nodeAttrGxy = mapper.createObjectNode();
			jsonItems.add(nodeAttrGxy);
			nodeAttrGxy.put("id",  item.first.getId());
			nodeAttrGxy.put("url", item.first.getUrl());
		}
	}
	
	static public void saveAttrGXY(XattrGXY attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("type", "GXY");
		userOb.put("id",   attr.getId());
		userOb.put("gid",  attr.getGId().getId());
		userOb.put("gurl", attr.getGId().getUrl());
		userOb.put("x",    attr.getXY().x);
		userOb.put("y",    attr.getXY().y);
	}
	
	static public void saveAttrSTR(XattrSTR attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("type",  "STR");
		userOb.put("name",  attr.getName());
		userOb.put("value", attr.getValue());
	}
	
	static public void saveAttrURL(XattrURL attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("type",  "URL");
		userOb.put("name",  attr.getName());
		userOb.put("value", attr.getValue());
	}
	
	static public void saveAttrLNK(XattrLNK attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("name", attr.getName());
		userOb.put("type", "LNK");
		userOb.put("first", attr.isFirst());
		userOb.put("id", attr.getId());
		userOb.put("oppositeId", attr.getOppositeId().getId());
		userOb.put("oppositeUrl", attr.getOppositeId().getUrl());
	}
	
	static public void saveAttrTPE(XattrTPE attr, ArrayNode jsonArray, ObjectMapper mapper) {
		ObjectNode userOb = mapper.createObjectNode();
		jsonArray.add(userOb);
		userOb.put("type", "TPE");
		userOb.put("id", attr.id);
	}
}
